package com.lucenten.mq;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;


public class MsgBusinessHandler {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	private static final HashMap<String, TSessionConnect> producerSessionMap; // 队列生产者SessionMap（key：队列名）
	private static final HashMap<String, TSessionConnect> consumerSessionMap; // 队列消费者SessionMap（key：队列名）
	
	public static String mQbrokerURL="";
	static {
		producerSessionMap = new HashMap<String, TSessionConnect>();
		consumerSessionMap = new HashMap<String, TSessionConnect>();
	}

	/**
	 * 将消息发送到队列中（客户端、座席端均使用该方法）
	 * @param map
	 * @throws Exception
	 */
	public static void sendMsgToQueue(Map map) throws Exception {
		String queueName =(String)map.get("queueName"); // 获取目标队列名称
		String queuText = (String)map.get("queuText"); // 获取目标队列名称
		TSessionConnect producerSession = producerSessionMap.get(queueName);
		if (producerSession == null || producerSession.getSession() == null || producerSession.getConnection() == null) {
			QueueMgrUtils.mQbrokerURL=mQbrokerURL;
			producerSession = QueueMgrUtils.createProducer(queueName);
			producerSessionMap.put(queueName, producerSession);// 将生产者放入HashMap中
		}
		QueueMgrUtils.sendMessageToQueue(producerSession, queuText); // 直接发送消息
	}

	/**
	 * 创建消息消费者
	 * 
	 * @param queueName
	 * @return
	 * @throws Exception
	 */
	public static TSessionConnect createConsumner(String queueName) throws Exception {
		TSessionConnect consumerSession = consumerSessionMap.get(queueName);
		if (consumerSession == null || consumerSession.getSession() == null || consumerSession.getConnection() == null) {
			QueueMgrUtils.mQbrokerURL=mQbrokerURL;
			consumerSession = QueueMgrUtils.createConsumer(queueName);
			consumerSessionMap.put(queueName, consumerSession);// 将消费者放入HashMap中
		}
		return consumerSession;
	}
	
	public void setMQbrokerURl(String mQbrokerURL){
		this.mQbrokerURL=mQbrokerURL;
	}
}
